angular.module('BaseControl')

.directive("dropdownlist", [function() {
    return {
        restrict: "E",	// 指令是一个元素 (并非属性)，备选项有"A"、"E" 和 "C"， "M" ,分别代表attribute、element、class和comment（默认值为"A"）
        scope: {
            value: '=',
            enum: '=?',
            change: '&',
            blur: '&',
            keydown: '&'
        },
        template: 		// 替换HTML (使用scope中的变量)
            '<select class="form-control BaseControl" ng-options="enumValue.Text for enumValue in EnumValues" ng-show="Visible" ng-click="selectClick()" ng-blur="textBlur()" ng-disabled="!Enabled" ng-model="EnumValue"></select>',
        replace: true,			// 使用模板替换原始标记
        transclude: true,		// 不复制原始HTML内容
        controller: ['$scope', function($scope){
            $scope.EnumValue;
            $scope.EnumValues;
            $scope.isTextChange = false;

            $scope.textChange = function(){
                $scope.value = $scope.EnumValue.Value;
                $scope.isTextChange = true;
                $scope.$parent.$digest();
                $scope.change();
            };
            $scope.textBlur = function(){
                $scope.blur();
            };
            $scope.keyDown = function(){
                $scope.keydown();
            };
        }],
        link: function (scope, element, attrs) {
            scope.Enabled = true;
            scope.Visible = true;
            scope.IsEmpty = true;
            scope.IsGrid = false;
            scope.BindField = "";
            scope._Dom = element[0];

            element[0].addEventListener('keydown', function(){ scope.keyDown(); }, false);

            if (attrs["isempty"] != undefined && attrs["isempty"].trim().toLocaleLowerCase() == "false") {
                scope.IsEmpty = false;
            }
            if (attrs["isgrid"] != undefined && attrs["isgrid"].toLocaleLowerCase() == "true") {
                scope.IsGrid = true;
            }
            if (attrs["bindfield"] != undefined && attrs["bindfield"].trim() != "") {
                scope.BindField = attrs["bindfield"].trim();
                if(scope.BindField.length > 0){
                    scope.$watch(
                        function(){
                            try{
                                return eval("scope.$parent." + scope.BindField);
                            }
                            catch(e){
                                return null;
                            }
                        },
                        function(){
                            var newValue;
                            try{
                                newValue = eval("scope.$parent." + scope.BindField);
                            }
                            catch(e){
                                newValue = null;
                            }
                            if(newValue != scope.value){
                                scope.EnumValue = undefined;
                                for(var i=0; i<scope.EnumValues.length; i++){
                                    if(scope.EnumValues[i].Value === newValue){
                                        scope.EnumValue = scope.EnumValues[i];
                                        break;
                                    }
                                }
                                window.setTimeout(function () {
                                    scope.textChange();
                                }, 1);
                            }
                        }
                    );
                }
            }

            element[0].addEventListener('change', function(){scope.textChange()}, false);

            scope.selectClick = function(){
                if(scope.IsEmpty){
                    if(scope._Dom.value != "?"){
                        //scope._Dom.options.add(new Option("","-1"), scope._Dom.options[0]);
                        var isFindEmpty = false;
                        for(var i=0; i<scope.EnumValues.length; i++){
                            if(scope.EnumValues[i].Value == undefined){
                                isFindEmpty = true;
                                break;
                            }
                        }
                        if(!isFindEmpty){
                            scope.EnumValues.splice(0, 0 ,{Value: undefined, Text: ''});
                        }
                    }
                }
            }
            
            scope.$watch(
                function(){
                    return JSON.stringify(scope.enum);
                },
                function(){
                    var isFind = false;
                    scope.EnumValues = [];
                    for(var option in scope.enum){
                        scope.EnumValues.push(scope.enum[option]);
                        if(scope.enum[option].Value === scope.value){
                            isFind = true;
                            scope.EnumValue = scope.enum[option];
                        }
                    }

                    if(!isFind){
                        scope.EnumValue = undefined;
                    }
                }
            );
            scope.$watch(
                function(){
                    return scope.value;
                },
                function(){
                    if(!scope.isTextChange){
                        scope.EnumValue = undefined;
                        for(var i=0; i<scope.EnumValues.length; i++){
                            if(scope.EnumValues[i].Value === scope.value){
                                scope.EnumValue = scope.EnumValues[i];
                                break;
                            }
                        }
                    }
                    else{
                        scope.isTextChange = false;
                    }
                }
            );
            scope.$watch(
                function(){
                    return attrs["enabled"];
                },
                function(){
                    if (attrs["enabled"] != undefined && attrs["enabled"].trim().toLocaleLowerCase() == "false") {
                        scope.Enabled = false;
                    }
                    else{
                        scope.Enabled = true;
                    }
                }
            );

            scope.$watch(
                function(){
                    return attrs["visible"]
                },
                function(){
                    if (attrs["visible"] != undefined && attrs["visible"].trim().toLocaleLowerCase() == "false") {
                        scope.Visible = false;
                    }
                    else{
                        scope.Visible = true;
                    }
                }
            );
        }
    }
}]);
